package com.ggk911.pharmacy_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggk911.pharmacy_system.entity.User;
import com.ggk911.pharmacy_system.service.impl.UserServiceImpl;
import com.ggk911.pharmacy_system.util.JWTUtil;
import com.ggk911.pharmacy_system.util.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户控制层
 *
 * @author ggk-911
 * @version 1.0 2023/3/8 10:51
 */
@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/user")
@Api(tags = "用户控制层")
@Slf4j
public class UserController {
    private final UserServiceImpl service;
    private final JWTUtil jwtUtil;

    //GET查询
    @GetMapping("/queryAll")
    @ApiOperation("查询所有用户")
    public RestResult queryAll(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size) {
        Page<User> userPage = new Page<>(page, size);

        QueryWrapper<User> wrapper = new QueryWrapper<>();


        service.page(userPage);
        return RestResult.ok("查询成功", userPage);
    }

    //GET获取所有数据集合
    @GetMapping("/list")
    public RestResult getList() {
        List<User> userList = service.list();
        return RestResult.ok("获取成功", userList);
    }

    //POST登录
    @PostMapping("/login")
    @ApiOperation("验证用户名密码")
    public RestResult login(@RequestParam("userName") @ApiParam("用户名") String userName,
                            @RequestParam("userPwd") @ApiParam("用户密码") String userPwd) {
        log.info("登录用户：" + userName + "登录密码：" + userPwd);
        User u = service.login(userName, userPwd);
        //登录错误
        if (u == null) return RestResult.error("登录错误，检查用户名或密码！");
        String token = jwtUtil.createToken(u);
        return RestResult.ok("登录成功", token);
    }

    //POST校验Token
    @PostMapping("/verifyToken")
    @ApiOperation("校验Token")
    public RestResult verifyToken(@RequestParam("token") @ApiParam("要验证的Token") String token) {
        return jwtUtil.verifyAndConvertToken(token);
    }

    //PUT插入
    @PutMapping("/insert")
    @ApiOperation("添加用户")
    public RestResult add(@ApiParam("添加用户") User user) {
        try {
            if (service.save(user)) return RestResult.ok("添加成功，用户编号为：" + user.getUserId(), user.getUserId());
        } catch (Exception e) {
            log.error(String.valueOf(e));
        }
        return RestResult.error("添加错误");
    }

    //DELETE删除
    @DeleteMapping("/delete")
    @ApiOperation("删除用户")
    public RestResult delete(Integer id) {
        try {
            if (service.removeById(id)) return RestResult.ok("删除成功");
        } catch (Exception e) {
            log.error(String.valueOf(e));
        }
        return RestResult.error("删除失败");
    }

    //POST更新
    @PostMapping("/update")
    @ApiOperation("更新用户")
    public RestResult update(User user) {
        try {
            if (service.updateById(user)) return RestResult.ok("更新成功");
        } catch (Exception e) {
            log.error(String.valueOf(e));
        }
        return RestResult.error("更新失败");
    }


}
